<style scoped>
.wraper {
    padding: 0 30px;
}

.search {
    padding-bottom: 20px;
}

.search span {
    color: #68777c;
    font-weight: bold;
    padding-right: 18px;
}

.search button {
    background-color: #00a8f7;
    color: #fff;
    margin-left: 8px;
}

.top {
    margin-bottom: 20px;
    border-top: 1px dashed #cccccc;
}

.detail {
    border-top: 1px dashed #cccccc;
}

.wraper .headline {
    position: relative;
    font-size: 12px;
    color: #303134;
    font-weight: bold;
    margin: 10px 0;
    line-height: 32px;
}

.wraper .add-agent {
    position: absolute;
    top: 0px;
    right: 30px;
    background-color: #348bf2;
    color: #fff;
}

.iconfont {
    font-size: 14px;
}

.red-font {
    color: #ff1f1f;
}








/* 表格样式 start*/

.pro-card {
    margin-bottom: 10px;
}

.pro-card-head {
    background-color: #e6e7eb;
    line-height: 38px;
    border: 1px solid #ddd;
}

.pro-card-title {
    font-size: 14px;
    color: #333;
    font-weight: bold;
    padding-left: 20px;
}

.pro-card-headr {
    padding-right: 10px;
    text-align: right;
}

.pro-card-content {
    clear: both;
}

.pro-paramname {
    text-align: center;
    background-color: #e6e7eb;
    font-size: 14px;
    color: #333;
    border-right: 1px solid #ddd;
}

.pro-paramvalue {
    padding-left: 30px;
    border-right: 1px solid #ddd;
}

.pro-card-contentrow {
    line-height: 40px;
    border-left: 1px solid #ddd;
    border-bottom: 1px solid #ddd;
}






/* 表格样式  end */


/* 分页 start */

.page {
    margin: 20px 0;
}

.ivu-page {
    padding-bottom: 150px;
}







/* 分页 end */
</style>

<template>
    <div class="policy-manage">
        <product-title></product-title>
        <div class="wraper">
            <div class="search">
                <span>快速搜索</span>
                <Select v-model="searchForm.type" placeholder="请输入类型"  style="width:140px">
                    <Option  value="0">全部</Option>
                    <Option  value="人寿险">人寿险</Option>
                    <Option  value="意外险">意外险</Option>
                    <Option  value="重疾险">重疾险</Option>
                    <Option  value="医疗险">医疗险</Option>
                    <Option  value="年金险">年金险</Option>
                    <Option  value="综合型">综合型</Option>
                </Select>
                <Input v-model="searchForm.num" placeholder="请输入保单号" style="width: 120px"></Input>
                <Input v-model="searchForm.appName" placeholder="请输入投保人" style="width: 120px"></Input>
                <Input v-model="searchForm.insuredName" placeholder="请输入被保人" style="width: 120px"></Input>
                <Input v-model="searchForm.benefitName" placeholder="请输入受益人" style="width: 120px"></Input>
                <Input v-model="searchForm.cname" placeholder="请输入公司名称" style="width: 120px"></Input>
                <Input v-model="searchForm.agent_name" placeholder="请输入代理人姓名" style="width: 140px"></Input>
                <Button @click='search'>查询</Button>
            </div>
            <div class="detail">
                <div class="headline">
                    <p>
                        <span class="red-font">提示</span>：1、此处为提示。
                    </p>
                    <Button class="add-agent" @click='showModal(0)'>新增计划书</Button>
                </div>
                <div class="clearfix">
                    <div class="card pro-card" v-for="(item,index) in policyList" :key="item.id">
                        <Row class="pro-card-head">
                            <Col span="12">
                            <Checkbox class="pro-card-title">{{item.name}} (保单号：{{item.num}})</Checkbox>
                            </Col>
                            <Col span="12" class="pro-card-headr">
                            <Button type="primary" size="small" @click="showRenewalModal(index)">续保统计</Button>
                            <Button type="primary" size="small" @click="showModal(item.num)">修改</Button>
                            <Button type="error" size="small" @click="delConfirm(index)">删除</Button>
                            </Col>
                        </Row>
                        <div class="pro-card-content">
                            <Row class="pro-card-contentrow">
                                <Col span="2" class="pro-paramname">投保人</Col>
                                <Col span="4" class="pro-paramvalue">{{item.appName}}</Col>
                                <Col span="2" class="pro-paramname">被保人</Col>
                                <Col span="4" class="pro-paramvalue">{{item.insuredName?item.insuredName:'暂无'}}</Col>
                                <Col span="2" class="pro-paramname">所属公司</Col>
                                <Col span="4" class="pro-paramvalue">{{item.cname}}</Col>
                                <Col span="2" class="pro-paramname">保费</Col>
                                <Col span="4" class="pro-paramvalue">{{item.totalRate}}元</Col>
                            </Row>
                            <Row class="pro-card-contentrow">
                                <Col span="2" class="pro-paramname">生效日期</Col>
                                <Col span="4" class="pro-paramvalue">{{item.inception_date?item.inception_date:'暂无'}}</Col>
                                <Col span="2" class="pro-paramname">代理人姓名</Col>
                                <Col span="4" class="pro-paramvalue">{{item.agent_name}}</Col>
                                <Col span="2" class="pro-paramname">保障期限</Col>
                                <Col span="10" class="pro-paramvalue">{{item.insurancePeriod?item.insurancePeriod:'暂无'}}</Col>
                            </Row>
                        </div>
                    </div>
                    <Row class="page fr">
                        <Col span="24">
                        <Page :total="policyTotal" show-total show-sizer @on-change="pageChange" @on-page-size-change="pageSizeChange"></Page>
                        </Col>
                    </Row>
                </div>
            </div>
        </div>

        <Modal v-model="renewalModal" :mask-closable='closeable' width="600" title="续保统计" @on-ok="reInsure" :loading="loading">
            <Row>
                <Col span="18" offset="1">
                <Form :model="renewalInsure" :label-width="120">
                    <Form-item label="保单编号：">
                        <Input v-model="renewalInsure.num" disabled placeholder="请输入保单编号"></Input>
                    </Form-item>
                    <Form-item label="投保人编号：">
                        <Input v-model="renewalInsure.agent_code" disabled placeholder="请输入投保人编号："></Input>
                    </Form-item>
                    <Form-item label="保费：">
                        <Input v-model="renewalInsure.rate" disabled placeholder="请输入保单编号"></Input>
                    </Form-item>
                    <Form-item label="续保时间" required>
                        <DatePicker type="date" v-model="renewalInsure.renewal_date" format="yyyy-MM-dd" @on-change="renewalDateChange" placeholder="选择生效日期" style="width: 306px"></DatePicker>
                    </Form-item>
                </Form>
                </Col>
            </Row>
        </Modal>
    </div>
</template>

<script>
import productTitle from '../../components/productTitle'
import { getPolicyList, delPolicy, renewal_insure } from '../../api/policy';
export default {
    data() {
        return {
            closeable: false,
            loading: true,
            page: 1,
            pagesize: 10,
            searchForm: {
                name: '',
                num: '',
                agent_code: ''
            },
            policyDetail: {

            },
            renewalInsure: {

            },
            renewalModal: false,
            rankList: [
                {
                    value: '1',
                    label: '1年交'
                },
                {
                    value: '2',
                    label: '2年交'
                },
                {
                    value: '3',
                    label: '3年交'
                },
                {
                    value: '4',
                    label: '5年交'
                },
                {
                    value: '5',
                    label: '10年交'
                },
                {
                    value: '6',
                    label: '15年交'
                },
                {
                    value: '7',
                    label: '20年交'
                },
            ],
            policyTotal: 0,
            policyList: [
            ],
        }
    },
    components: {
        productTitle
    },
    created() {
        this.getList();
    },
    methods: {
        // 搜索
        search() {
            this.getList();
        },
        // 获取员工列表
        getList() {
            let _params = {
                'p': this.page,
                'pagesize': this.pagesize,
                'type': this.searchForm.type,
                'insuredName': this.searchForm.insuredName,
                'benefitName': this.searchForm.benefitName,
                'appName': this.searchForm.appName,
                'cname': this.searchForm.cname,
                'agent_name': this.searchForm.agent_name,
                'num': this.searchForm.num,
            }
            new Promise((resolve, reject) => {
                getPolicyList(_params).then(response => {
                    if (response.data.err == 0) {
                        this.policyList = response.data.data.list;
                        this.policyTotal = response.data.data.total;
                    } else {
                        this.$Message.error(response.data.info);
                    }
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
        },
        // 页码改变
        pageChange(page) {
            this.page = page
            this.getList()
        },
        // 每页条数改变
        pageSizeChange(pagesize) {
            this.pagesize = pagesize
            this.getList();
        },

        add() {
            this.$refs[this.policyDetail].validate((valid) => {
                if (valid) {
                    new Promise((resolve, reject) => {
                        savePolicy(this.policyDetail).then(response => {
                            if (response.data.err == 0) {
                                this.$Message.success(response.data.info);
                                this.showAddModal = false;
                                this.loading = true;
                                this.getList();
                            } else {
                                this.$Message.error(response.data.info);
                                setTimeout(() => {
                                    this.loading = false;
                                    this.$nextTick(() => {
                                        this.loading = true;
                                    });
                                }, 2000);
                            }
                            resolve();
                        }).catch(error => {
                            reject(error);
                        });
                    });
                } else {
                    // 通过loading来阻止modal隐藏，然后手动修改loading来充值确定按钮
                    setTimeout(() => {
                        this.loading = false;
                        this.$nextTick(() => {
                            this.loading = true;
                        });
                    }, 2000);
                }
            })
        },
        del(param) {
            new Promise((resolve, reject) => {
                delPolicy(param).then(response => {
                    if (response.data.err == 0) {
                        this.$Message.success(response.data.info);
                        this.getList();
                    } else {
                        this.$Message.error(response.data.info);
                    }
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
        },
        //添加or编辑代理人，打开modal
        showModal(id) {
            this.$router.push({ name: 'plan_edit', params: { id: id } });
        },
        // 删除时弹出对话框
        delConfirm(index) {
            this.$Modal.confirm({
                title: '提示',
                content: '是否要删除该保单?',
                onOk: () => {
                    let _params = {
                        'num': this.policyList[index].num
                    }
                    this.del(_params);
                },
            });
        },
        // 监测datepicker的on-change时间，date为返回值，是格式化后的日期
        startDateChange(date) {
            this.policyDetail.start_date = date;
        },
        renewalDateChange(date) {
            this.renewalInsure.renewal_date_format = date;
        },
        // 续保时弹出modal
        showRenewalModal(index) {
            this.renewalModal = !this.renewalModal;
            this.renewalInsure.num = this.policyList[index].num;
            this.renewalInsure.rate = this.policyList[index].rate;
            this.renewalInsure.agent_code = this.policyList[index].agent_code;
        },
        // 续保
        reInsure() {
            new Promise((resolve, reject) => {
                renewal_insure(this.renewalInsure).then(response => {
                    if (response.data.err == 0) {
                        this.$Message.success(response.data.info);
                        this.showAddModal = false;
                        this.loading = true;
                    } else {
                        this.$Message.error(response.data.info);
                        setTimeout(() => {
                            this.loading = false;
                            this.$nextTick(() => {
                                this.loading = true;
                            });
                        }, 2000);
                    }
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
        }
    }
}
</script>
